package com.gxar.quick.ar.server.base.controller;

import com.gxar.common.entity.Result;
import com.gxar.common.entity.SlippageResult;
import com.gxar.common.entity.SystemCodeEnum;
import com.gxar.common.entity.login.LoginUserThread;
import com.gxar.common.entity.login.UserProfile;
import com.gxar.common.exception.APIRuntimeException;
import com.gxar.quick.ar.server.base.common.enums.UserWorksTypeEnum;
import com.gxar.quick.ar.server.base.model.plaza.vo.response.PlazaForwardResponse;
import com.gxar.quick.ar.server.base.model.plaza.vo.response.PlazaInfoResponse;
import com.gxar.quick.ar.server.base.model.plaza.vo.response.PlazaLikeResponse;
import com.gxar.quick.ar.server.base.model.plaza.vo.response.PlazaListResponse;
import com.gxar.quick.ar.server.base.model.works.vo.response.WorksInfoResponse;
import com.gxar.quick.ar.server.db.service.PlazaService;
import com.gxar.quick.ar.server.db.service.WorksService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * @author linsy
 * @version 1.0
 * @created 2023/11/8 09:20
 */
@Api(tags = "广场接口")
@RestController
@RequestMapping("/api/plazas")
@RequiredArgsConstructor
@Slf4j
public class PlazaController {

    private final PlazaService plazaService;

    @ApiOperation("广场列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "next_id", value = "下一个查询的id", dataType = "string"),
            @ApiImplicitParam(name = "type", value = "广场类型：favorites-我收藏的，AR作品分享页可以为空", dataType = "string"),
    })
    @GetMapping("")
    public Result<SlippageResult<PlazaListResponse>> getList(@RequestParam(name = "next_id", required = false) String nextId,
                                                             @RequestParam(name = "type", required = false) String type) {
        UserProfile userProfile = LoginUserThread.get();
        UserWorksTypeEnum userWorksTypeEnum = UserWorksTypeEnum.MINE;
        if (StringUtils.isNotBlank(type)) {
            userWorksTypeEnum = UserWorksTypeEnum.getByName(type);
        }
        if (UserWorksTypeEnum.FAVORITES.equals(userWorksTypeEnum) && Objects.isNull(userProfile)) {
            throw new APIRuntimeException(SystemCodeEnum.UN_AUTH);
        }
        SlippageResult<PlazaListResponse> plazaListResponse = plazaService.list(userProfile, nextId, userWorksTypeEnum);
        return Result.success(plazaListResponse);
    }

    @ApiOperation("广场作品收藏")
    @PostMapping("/{id}/favorites")
    public Result<PlazaLikeResponse> favorites(@PathVariable("id") Long id) {
        UserProfile userProfile = LoginUserThread.get();
        if (Objects.isNull(userProfile)) {
            throw new APIRuntimeException(SystemCodeEnum.UN_AUTH);
        }
        PlazaLikeResponse plazaLikeResponse = plazaService.favorites(userProfile, id);
        return Result.success(plazaLikeResponse);
    }

    @ApiOperation("广场作品分享")
    @PostMapping("/{id}/share")
    public Result<PlazaForwardResponse> share(@PathVariable("id") Long id) {
        UserProfile userProfile = LoginUserThread.get();
        PlazaForwardResponse plazaForwardResponse = plazaService.share(userProfile, id);
        return Result.success(plazaForwardResponse);
    }

    @ApiOperation("查看广场分享详情")
    @ApiImplicitParam(name = "id", value = "作品id", dataType = "long", required = true, paramType = "path")
    @GetMapping("/{id}")
    public Result<PlazaInfoResponse> getDetail(@PathVariable(name = "id") Long id) {
        UserProfile userProfile = LoginUserThread.get();
        return Result.success(plazaService.getDetail(userProfile, id));
    }

    @ApiOperation("查看广场作品详情（制作同款使用）")
    @ApiImplicitParam(name = "id", value = "作品id", dataType = "long", required = true, paramType = "path")
    @GetMapping("/works/{id}")
    public Result<WorksInfoResponse> getWorksDetail(@PathVariable(name = "id") Long id) {
        UserProfile userProfile = LoginUserThread.get();
        return plazaService.getWorksDetail(userProfile, id);
    }

}
